package study

import (
	"context"
	"encoding/json"
	"ez/apps/study/auto/mc"
	"gitee.com/dreamwood/ez-go/db/mgo"
	"gitee.com/dreamwood/ez-go/ez"
	"gitee.com/dreamwood/ez-go/ss"
	"time"
)

type ReadWrite struct {
	mgo.BaseDoc `bson:"-" json:"-"`
	Id          int64 `json:"id" bson:"id,omitempty"`

	Read string `json:"read" bson:"read"` //读

	Write string `json:"write" bson:"write"` //写

	Sense string `json:"sense" bson:"sense"` //义

	Note string `json:"note" bson:"note"` //说明

	Subject *Subject `json:"subject" bson:"subject"` //学科

	SubjectId int64 `json:"subjectId" bson:"subjectId"` //学科

	Grade *Grade `json:"grade" bson:"grade"` //年级

	GradeId int64 `json:"gradeId" bson:"gradeId"` //年级

	Knowledge *Knowledge `json:"knowledge" bson:"knowledge"` //知识点

	KnowledgeId int64 `json:"knowledgeId" bson:"knowledgeId"` //知识点

	Mp3 string `json:"mp3" bson:"mp3"` //音频

	CreateAt time.Time  `json:"createAt" bson:"createAt"`
	UpdateAt time.Time  `json:"updateAt" bson:"updateAt"`
	DeleteAt *time.Time `json:"deleteAt" bson:"deleteAt"`
}

func (this *ReadWrite) DocName() string { return "ReadWrite" }

func (this *ReadWrite) GetId() int64 { return this.Id }

func (this *ReadWrite) SetId(id int64) { this.Id = id }

func (this *ReadWrite) Create() error {
	return this.GetFactory().Create(this)
}

func (this *ReadWrite) Replace() error {
	return this.GetFactory().Replace(this)
}

func (this *ReadWrite) Save() error {
	if this.Id == 0 {
		this.CreateAt = time.Now()
		this.UpdateAt = time.Now()
		return this.GetFactory().Create(this)
	} else {
		this.UpdateAt = time.Now()
		return this.GetFactory().Replace(this)
	}
}

// 伪删除
func (this *ReadWrite) Delete() error {
	return this.GetFactory().Delete(this)
}

func (this *ReadWrite) UnDelete() error {
	return this.GetFactory().UnDelete(this)
}

// 真删除
func (this *ReadWrite) Destroy() error {
	return this.GetFactory().Destroy(this)
}

func (this *ReadWrite) ToString() string {
	return string(this.ToBytes())
}

func (this *ReadWrite) ToBytes() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ReadWrite) Serialize() []byte {
	data, e := json.Marshal(this)
	ez.Try(e)
	return data
}
func (this *ReadWrite) UnSerialize(data []byte) []byte {
	ez.Try(json.Unmarshal(data, this))
	return data
}

func (this *ReadWrite) LoadSubject() {
	if this.SubjectId == 0 {
		return
	}
	this.Subject, _ = NewSubjectCrud().FindId(this.SubjectId)
}

func (this *ReadWrite) LoadGrade() {
	if this.GradeId == 0 {
		return
	}
	this.Grade, _ = NewGradeCrud().FindId(this.GradeId)
}

func (this *ReadWrite) LoadKnowledge() {
	if this.KnowledgeId == 0 {
		return
	}
	this.Knowledge, _ = NewKnowledgeCrud().FindId(this.KnowledgeId)
}

func (this *ReadWrite) ClearRelationsBeforeSave() mgo.Doc {

	this.Subject = nil

	this.Grade = nil

	this.Knowledge = nil

	return this
}

func neverUsed_ReadWrite() {
	//导入ss包
	a := ss.M{}
	ez.Debug(a)
}

type ReadWriteAccessControl struct {
	Access  bool
	Message string
	Action  string //控制器Action,小写开头，如c,u,r,d
	Model   *ReadWrite
	Session *ez.Session
}

func NewReadWriteAccessControl(model *ReadWrite, action string, session *ez.Session) *ReadWriteAccessControl {
	ctrl := &ReadWriteAccessControl{
		Access:  true,
		Model:   model,
		Action:  action,
		Session: session,
	}
	ez.DispatchToMany(mc.ReadWriteAccessControlEvent, ctrl, context.TODO())
	return ctrl
}
